#include <stdio.h>

#define MAX_SIEVE 100000
#define NOT_PRIME (-1)

#define NUM_PER_ROW 10

unsigned long long sieve[MAX_SIEVE];
int i, j;

int main(int argc, char* argv[])
{
	/** Find primes. */
	printf("Finding primes\n");
	for (i = 0; i < MAX_SIEVE; i++)
		sieve[i] = i+2;
	
	for (i = 0; i < MAX_SIEVE; i++)
	{
		printf("i = %d\n", i);
		if (sieve[i] == NOT_PRIME) continue;
		for (j = i+1; j < MAX_SIEVE; j++)
		{
			if (sieve[j] % sieve[i] == 0) sieve[j] = NOT_PRIME;
		}
	}

	unsigned long long num;
	/** Factorization. */
	while (1)
	{
		printf("Give number: ");
		fflush(stdout);
		scanf("%llu", &num);
		for (i = 0; i < MAX_SIEVE; i++)
		{
			if (sieve[i] == NOT_PRIME) continue;
			j = 0;
			while (num % sieve[i] == 0) { j++; num /= sieve[i]; }
			if (j > 0)
			{
				printf("N = %llu, p = %llu, times_count = %u\n", num, sieve[i], j);
				fflush(stdout);
			}
			if (num == 1) break;
		}
	}
}
